﻿#include "MarketService.h"
#include<fstream>
#include<iostream>
#include "Tools.h"
//#include "json/reader.h"
//#include "WaveFormJson.h"
//#include "OrderTrader.h"
//#include "OrderData.h"
#include "TradeApiSpi.h"
#include <list>
#include <string>
#include "FileOperator.h"
//#include "WaveTurnRoundStrategy.h"
#include "MarketDataStream.h"
#include "MarketDataSource.h"

MarketService::MarketService(): Trader(nullptr)
{
	//m_workspace = Tools::GetWorkSpace();
	m_lastInsertOrderTime = Tools::GetCurrentTimeStamp();
	m_ctpDisconnectTime = 0;
	init();
	setTradeHandle(&m_ctpModel);
}

MarketService::~MarketService()
{
}

void MarketService::DealFrameDataList(const std::list<FData::FrameKItem> &pframeList)
{
	for (std::list<FData::FrameKItem>::const_iterator itK = pframeList.begin(); itK != pframeList.end(); ++itK)
	{
		const FData::FrameKItem &fkItem = *itK;
		OnDealMarket(fkItem.SymbolName, fkItem.LastKItemGenTime, fkItem.priceItem);
	}
}

bool MarketService::IsCTPConnected()
{
	if (m_ctpModel.GetQueryError() != 0)
	{
		return false;
	}
	if (!m_ctpModel.GetCTPStatus())
	{
		if (m_ctpDisconnectTime == 0)
		{
			m_ctpDisconnectTime = Tools::GetCurrentTimeStamp();
			return true;
		}
		if (Tools::GetCurrentTimeStamp() - m_ctpDisconnectTime > 300)
		{
			return false;
		}
	}
	else
	{
		m_ctpDisconnectTime = 0;
	}
	return true;
}

void MarketService::InsertCTPOrder(const TD::OrderOption &pOption, TD::OrderBackFunc pFunc)
{
	m_TradeHandle->InsertOrderNow(pOption, pFunc);
}

//void MarketService::onFrameTradeOrder(const std::string &pSymbol, MData::PriceItem &priceItem, OrderSupport::PrepareData *supportData, FrameTradeOrder *pTradeOrder)
//{
//	if (supportData == nullptr || pTradeOrder == nullptr)
//	{
//		return;
//	}
//	// 这里是要出场了
//	if (pTradeOrder->TOrder.ExitData.StrategyValue > 0.0f)
//	{
//		if (pTradeOrder->TOrder.EnterData.RealValue > 0.0f && pTradeOrder->TOrder.ExitData.RealValue <= 0.0f)
//		{
//			closeOrder(pSymbol, *supportData, *pTradeOrder, priceItem);
//		}
//		return;
//	}
//	// 判断入场
//	if (pTradeOrder->TOrder.EnterData.StrategyValue > 0.0f)
//	{
//		if (pTradeOrder->TOrder.EnterData.RealValue <= 0.0f)
//		{
//			openOrder(pSymbol, *supportData, *pTradeOrder, priceItem);
//		}
//	}
//}

void MarketService::init()
{
	m_ctpModel.Init();
	m_ctpModel.QueryAccount();
	m_ctpModel.QueryOrders();
	m_ctpModel.QueryPositions("", nullptr);
	m_ctpModel.QueryTrades();

	//// 设置下单成功回调函数
	//m_ctpModel.SetFeedbackFunc([this](const std::string &pSymbol, float orderPrice, char pTimeStr[9]) {
	//	tradeFeedback(pSymbol, orderPrice, pTimeStr);
	//});
}
//
//void MarketService::closeOrder(const std::string &pSymbol, const OrderSupport::PrepareData &supportData, const Trader::FrameTradeOrder &pTradeOrder, MData::PriceItem &pItem)
//{
//	// 如果已经下单了才可以平仓，并且下单间隔不能太紧
//	if (pItem.UpdateTime - m_lastInsertOrderTime < 2)
//	{
//		m_ctpModel.InsertOrderNow(pSymbol, 0, pTradeOrder.GetTickCount(), pItem.LastPrice);
//		m_lastInsertOrderTime = pItem.UpdateTime;
//	}
//}
//
//void MarketService::openOrder(const std::string &pSymbol, const OrderSupport::PrepareData &supportData, const Trader::FrameTradeOrder &pTradeOrder, MData::PriceItem &pItem)
//{
//	if (pItem.UpdateTime - m_lastInsertOrderTime < 2)
//	{
//		// 开仓时间与拐点时间不能太远
//		m_ctpModel.InsertOrderNow(pSymbol, supportData.mTrend == OrderSupport::Trend_UP ? 1 : -1, pTradeOrder.GetTickCount(), pItem.LastPrice);
//		m_lastInsertOrderTime = pItem.UpdateTime;
//	}
//}

//void MarketService::tradeFeedback(const std::string &pSymbol, float orderPrice, char pTimeStr[9])
//{
//	tm lastinsertTm = Tools::GetDateTimeFrmStamp(m_lastInsertOrderTime);
//	time_t curTMStamp = MDSrc::GetDateTime(lastinsertTm, pTimeStr);
//	if (curTMStamp < m_lastInsertOrderTime) // 证明时间已经是第二天了
//	{
//		lastinsertTm = Tools::AddDays(lastinsertTm, 1);
//		curTMStamp = MDSrc::GetDateTime(lastinsertTm, pTimeStr);
//	}
//
//	modifyRealOrdetValue(pSymbol, orderPrice, curTMStamp);
//
//	m_lastInsertOrderTime = curTMStamp;
//}